﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace SisLegal.Model
{
    public class ViaProcedural
    {
        public int ID { get; set; }
        public int ProcessID { get; set; }
        public string ProcessName { get; set; }
        public string Name { get; set; }

        public ViaProcedural()
	    {
            this.ID = 0;
            this.ProcessID = 0;
            this.ProcessName = "";
            this.Name = "";
	    }
    }

    public class ViaProceduralManager
    {
        
        public Int32 Count()
        {
            string sqlQuery = String.Format("  SELECT COUNT(ID) AS total FROM ViaProcedural");
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            command.CommandType = CommandType.Text;
            Int32 countStart = (Int32)command.ExecuteScalar();
            return countStart;
        }

        public List<ViaProcedural> Paginate(Int32 pag)
        {
            List<ViaProcedural> result = new List<ViaProcedural>();
            string sqlQuery = String.Format("SELECT * FROM (" +
                "SELECT ROW_NUMBER() OVER(ORDER BY ID) AS NUMBER, * FROM ViaProcedural " +
                ")as tbl " +
                "WHERE NUMBER BETWEEN((" + Convert.ToString(pag) + "-1) * (10 +1)) AND (" + Convert.ToString(pag) + " * 10) ORDER BY ID");

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            ViaProcedural process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new ViaProcedural();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    process.ProcessID = Convert.ToInt32(dataReader["ProcessID"]);
                    process.ProcessName = ((eTypeProcess)process.ProcessID).ToString();

                    result.Add(process);
                }
            }
            return result;
        }

        public int Save(ViaProcedural obj)
        {
            string createQuery = String.Format("Insert into ViaProcedural (Name, ProcessID) Values('{0}','{1}');"
                + "Select @@Identity", obj.Name, obj.ProcessID);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;

            command = new SqlCommand(createQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
            }
            catch (Exception ex)
            {

            }

            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }



        public List<ViaProcedural> GetByProcessID(Int32 ProcessID)
        {
            List<ViaProcedural> result = new List<ViaProcedural>();
            string sqlQuery = String.Format("SELECT *  FROM ViaProcedural WHERE ProcessID = " + Convert.ToString(ProcessID) + " ORDER BY ID");

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();
            ViaProcedural process = null;
            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    process = new ViaProcedural();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.Name = dataReader["Name"].ToString();
                    //process.ProcessID = Convert.ToInt32(dataReader["ProcessID"]);
                    //process.ProcessName = ((eTypeProcess)process.ProcessID).ToString();

                    result.Add(process);
                }
            }
            return result;
        }

        /*public int Update(FiscalFolder obj)
        {
            string updateQuery = String.Format("Update FiscalFolder SET Name='{0}' Where ID = {1};",
               obj.Name, obj.ID);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = null;
            command = new SqlCommand(updateQuery, connection);

            int saveID = 0;
            try
            {
                var commandResult = command.ExecuteScalar();
                if (commandResult != null)
                {
                    saveID = Convert.ToInt32(commandResult);
                }
                else
                {
                    saveID = obj.ID;
                }
            }
            catch (Exception ex)
            {

            }
            command.Dispose();
            connection.Close();
            connection.Dispose();

            return saveID;
        }*/
        /*
        public FiscalFolder GetById(int articleId)
        {
            FiscalFolder process = new FiscalFolder();
            string sqlQuery = String.Format("select * from FiscalFolder where ID={0}", articleId);
            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader dataReader = command.ExecuteReader();

            if (dataReader.HasRows)
            {
                while (dataReader.Read())
                {
                    //result.ID = Convert.ToInt32(dataReader["ID"]);
                    //process = new FiscalFolder();
                    process.ID = Convert.ToInt32(dataReader["ID"]);
                    process.NroFiscalFolder = dataReader["NroFiscalFolder"].ToString();
                    process.Year = Convert.ToInt32(dataReader["Year"]);
                    process.Fiscalia = dataReader["Fiscalia"].ToString();
                    process.FiscalCargo = dataReader["FiscalCargo"].ToString();
                    process.FechaHechos = new DateTime();
                    process.FechaNoticicacionSeal = new DateTime();
                    process.FechaInicio = new DateTime();
                    process.MontoPretension = Convert.ToDecimal(dataReader["MontoPretension"]);
                    process.ParticipationSeal = Convert.ToInt32(dataReader["ParticipationSeal"]);
                    process.Article = Convert.ToInt32(dataReader["Article"]);
                    process.Money = Convert.ToInt32(dataReader["Money"]);
                    process.TypeProcess = Convert.ToInt32(dataReader["TypeProcess"]);
                    process.ContingencyRate = Convert.ToInt32(dataReader["ContingencyRate"]);
                    process.PoliciaCargo = dataReader["PoliciaCargo"].ToString();
                    process.Denunciante = dataReader["Denunciante"].ToString();
                    process.Denunciado = dataReader["Denunciado"].ToString();
                    process.Agraviado = dataReader["Agraviado"].ToString();
                    process.DocumentoInicial = dataReader["DocumentoInicial"].ToString();
                    process.Sumilla = dataReader["Sumilla"].ToString();
                    process.Observaciones = dataReader["Observaciones"].ToString();
                    //---------------------------------------------
                    process.AssignedPerson = Convert.ToInt32(dataReader["AssignedPerson"]);
                    process.Lawyer = Convert.ToInt32(dataReader["Lawyer"]);
                    process.OrdenServicio = dataReader["OrdenServicio"].ToString();
                    process.NroRecibo = dataReader["NroRecibo"].ToString();
                    process.TerminosReferencia = dataReader["TerminosReferencia"].ToString();
                    process.AbogadoSeal = dataReader["AbogadoSeal"].ToString();
                    process.PracticanteSeal = dataReader["PracticanteSeal"].ToString();
                    process.PhysicalLocationFiles = Convert.ToInt32(dataReader["PhysicalLocationFiles"]);

                }
            }

            return process;
        }

        public bool Delete(int articleId)
        {
            bool result = false;
            string sqlQuery = String.Format("delete from FiscalFolder where ID = {0}", articleId);

            SqlConnection connection = new SqlConnection(Util.ConnKLegal);
            connection.Open();
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            int rowsDeletedCount = command.ExecuteNonQuery();
            if (rowsDeletedCount != 0)
                result = true;
            command.Dispose();
            connection.Close();
            connection.Dispose();
            return result;
        }*/
    }
}